﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DatabaseSynchronizer {

    public partial class SettingsForm : BaseForm {

        public static string TFSServerURL;
        private Settings _settings = null;


        private void DisplayErrors(List<string> errors) {

            if (errors.Count == 0) {
                ErrorLabel.Text = string.Empty;
                ErrorLabel.Visible = false;
            }
            else {

                string error = string.Empty;
                errors.ForEach(e => error += (e + Environment.NewLine));
                ErrorLabel.Text = error;
                ErrorLabel.Visible = true;
            }
        }
        private void DisplayError(string error) {
            if (!string.IsNullOrEmpty(error)) {
                DisplayErrors(new List<string> { error });
            }
        }


        private void SettingsForm_Load(object sender, EventArgs e) {

            _settings = SettingsService.GetSettings(Environment.UserName, Environment.MachineName);
            ConnectionStringTextBox.Text = _settings.DisplayConnectionString;
            LocalDeltaScriptsLabel.Text = _settings.LocalScriptsFolder;
            DeleteSettingsButton.Enabled = !string.IsNullOrEmpty(_settings.ConnectionString);
        }

        public SettingsForm() {
            InitializeComponent();
        }


        private void SaveButton_Click(object sender, EventArgs e) {

            List<string> errors;
            string error = string.Empty;

            if (!ConnectionStringTextBox.ReadOnly) {
                _settings.ConnectionString = ConnectionStringTextBox.Text;
            }

            if (SettingsService.IsValid(_settings, out errors)
                && SettingsService.SaveSettings(_settings, out error)) {

                DialogResult = DialogResult.OK;
                Close();
            }
            else {
                if (errors != null && errors.Count() > 0) {
                    DisplayErrors(errors);
                }
                else {
                    DisplayError(error);
                }
            }
        }

        private void CancelButton_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void SelectServerButton_Click(object sender, EventArgs e) {

            Form settings = new SelectServer();
            DialogResult dialogResult = settings.ShowDialog();
            //Maybe later for TFS
        }


        private void SelectServerFolderButton_Click(object sender, EventArgs e) {

            Form settings = new SelectFolder(TFSServerURL);
            DialogResult dialogResult = settings.ShowDialog(this);
        }

        private void SelectLocalFolderButton_Click(object sender, EventArgs e) {

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) {
                _settings.LocalScriptsFolder = folderBrowserDialog1.SelectedPath;
                LocalDeltaScriptsLabel.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void EditConnStrButton_Click(object sender, EventArgs e) {

            if (ConnectionStringTextBox.ReadOnly) {

                EditConnStrButton.Text = "Done";
                ConnectionStringTextBox.ReadOnly = false;
                ConnectionStringTextBox.Text = _settings.ConnectionString;
            }
            else {

                _settings.ConnectionString = ConnectionStringTextBox.Text;
                ConnectionStringTextBox.Text = _settings.DisplayConnectionString;
                EditConnStrButton.Text = "Edit";
                ConnectionStringTextBox.ReadOnly = true;

            }
        }

        private void DeleteSettingsButton_Click(object sender, EventArgs e) {

            if (MessageBox.Show("Are you sure you want to delete your settings?" + Environment.NewLine +
                    "Deleting settings will not make any change to the database."
                    , "Delete settings?", MessageBoxButtons.YesNo) == DialogResult.Yes) {

                string errorMsg = string.Empty;
                if (!SettingsService.DeleteSettings(out errorMsg)) {
                    ErrorLabel.Text = errorMsg;
                }
                else {
                    ErrorLabel.Text = string.Empty;
                    DialogResult = DialogResult.OK;
                    Close();
                }
            }

        }
    }
}